package com.base.common.util;

/**
 * @author yanjiaqiang
 * @date 2010-12-22
 * @version $Id: ByteUtils.java 7 2011-07-15 04:24:04Z JiaqiangYan@gmail.com $
 */
public class ByteUtils {
	/**
	 * 将16进制的hex转化为对应的字节数组。<br>
	 * 比如"11"-> byte[]{17} <br>
	 * "ff" -> byte[]{-1} <br>
	 * "11ff"->byte[]{17,0} <br>
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] fromHex(String hex) {
		int len = hex.length();
		if (len % 2 != 0) {
			throw new IllegalArgumentException(" str is not a hex String, since it's len is " + len);
		}
		char[] chs = hex.toCharArray();
		byte[] bs = new byte[len / 2];
		for (int i = 0; i < len; i = i + 2) {
			int high = Character.digit(chs[i], 16);
			int low = Character.digit(chs[i + 1], 16);
			byte b = (byte) ((high << 4) | low);
			bs[i / 2] = b;
		}
		return bs;
	}
}
